package DAO;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import Utils.Http;

import Beans.ConfUpdate;
import Beans.Config;

public class IPDao {
	private Config config = null;
	ConfigDao configDao = new ConfigDao();
	List<Config> configs = configDao.getAllconfig();

	public String getIP(String name) {
		InetAddress address = null;
		try {
			address = InetAddress.getByName(name);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			System.out.println("获取失败");
		}
		return address.getHostAddress().toString();
	}

	@SuppressWarnings("deprecation")
	public void doPost(int terminalId, int status) {
		try {
			config = configs.get(0);
			String url = config.getValue()+"controller";
			HttpPost httpRequest = new HttpPost(url);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("funcID", "0021"));
			params.add(new BasicNameValuePair("status", "" + status));
			params.add(new BasicNameValuePair("terminalId", "" + terminalId));
			/* 添加请求参数到请求对象 */
			httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			/* 发送请求并等待响应 */
			// HttpResponse httpResponse =
			new DefaultHttpClient().execute(httpRequest);
			// /*若状态码为200 ok*/
			// if(httpResponse.getStatusLine().getStatusCode() == 200)
			// {
			// /*读返回数据*/
			// String strResult =
			// EntityUtils.toString(httpResponse.getEntity());
			// mTextView1.setText(strResult);
			// }
			// else
			// {
			// mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString());
			// }
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		} catch (Exception e) {
		}
	}

	public void doUpdate(int terminalId, int updateId) {
		ConfUpdateDao confUpdateDao = new ConfUpdateDao();
		ConfUpdate confUpdate = confUpdateDao.getConfUpdateById(updateId);
		try {
			config = configs.get(0);
			String url = config.getValue()+"controller";
//			String url = "http://localhost:8080/YouBoxApi/controller";
//			HttpPost httpRequest = new HttpPost(url);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("terminalId", "" + terminalId));
			params.add(new BasicNameValuePair("ip", "" + confUpdate.getIp()));
			params.add(new BasicNameValuePair("port", "" + confUpdate.getPort()));
			params.add(new BasicNameValuePair("ftpname", ""
					+ confUpdate.getFtpName()));
			params.add(new BasicNameValuePair("ftppwd", ""
					+ confUpdate.getFtpPwd()));
			params.add(new BasicNameValuePair("addr", "" + confUpdate.getAddr()));
			params.add(new BasicNameValuePair("savePath", "" + confUpdate.getSavepath()));
			params.add(new BasicNameValuePair("funcID", "0022"));
			/* 添加请求参数到请求对象 */
//			httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			/* 发送请求并等待响应 */
			// HttpResponse httpResponse =
//			new DefaultHttpClient().execute(httpRequest);
			Http.postByPair(url, params);
			// /*若状态码为200 ok*/
			// if(httpResponse.getStatusLine().getStatusCode() == 200)
			// {
			// /*读返回数据*/
			// String strResult =
			// EntityUtils.toString(httpResponse.getEntity());
			// mTextView1.setText(strResult);
			// }
			// else
			// {
			// mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString());
			// }
		}catch (Exception e) {
		}
	}

	public Config getConfig() {
		return config;
	}

	public void setConfig(Config config) {
		this.config = config;
	}
	
}
